package com.example.myhttplibrary.base;

import com.example.myhttplibrary.exceptiopn.ApiException;

import io.reactivex.Observer;
import io.reactivex.disposables.CompositeDisposable;
import io.reactivex.disposables.Disposable;
import retrofit2.HttpException;

public abstract class BaseObserver<T> implements Observer<T> {

    private CompositeDisposable compositeDisposable=new CompositeDisposable();
    @Override
    public void onSubscribe(Disposable d) {
        compositeDisposable.add(d);
    }

    @Override
    public void onNext(T t) {
        onSucess(t);
    }

    @Override
    public void onError(Throwable e) {
        compositeDisposable.clear();
        ApiException apiExipection = new ApiException();
        if(e instanceof HttpException){
            apiExipection.setMsg("网络异常");
            apiExipection.setCode(1001);
        }
        onError(apiExipection);
    }

    @Override
    public void onComplete() {

    }
    public abstract void  onSucess(T t);
    abstract void  onError(ApiException apiException);

}
